package org.hgvc.webapp.ets.jsf.beans.partner;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.CalendarCategory;
import org.hgvc.webapp.ets.bo.Partner;
import org.hgvc.webapp.ets.bo.PartnerProduct;
import org.hgvc.webapp.ets.bo.PartnerProductType;
import org.hgvc.webapp.ets.bo.XSServiceType;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class PartnerProductMgtBean extends AbstractBean {
	
	public static final String BEAN_NAME="partnerProductMgtBean";
	public static final String NAV_LIST_PRODUCT="partnerProductMgmt";
	public static final String NAV_CREATION="partnerProductCreate";
	private PartnerProduct product;
	private Partner partner;
	private PartnerService service;
	private List<PartnerProduct> productList;
	private List<SelectItem> typeList;
	private List<SelectItem> catList;
	private List<SelectItem> serviceTypeList;
	private String typeId;
	private String catId;
	
	
	
	public List<SelectItem> getServiceTypeList() {
		return serviceTypeList;
	}
	
	public PartnerProduct getProduct() {
		return product;
	}
	public void setProduct(PartnerProduct product) {
		this.product = product;
	}
	public Partner getPartner() {
		return partner;
	}
	public void setPartner(Partner partner) {
		this.partner = partner;
	}
	public void setService(PartnerService service) {
		this.service = service;
	}
	public String getTypeId() {
		return typeId;
	}
	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
	public String getCatId() {
		return catId;
	}
	public void setCatId(String catId) {
		this.catId = catId;
	}
	/*----------------------------------------------------------------------------*/
	
	public List<SelectItem> getTypeList() {
		try{
			List<PartnerProductType> list = service.getPartnerProductTypeList();
			typeList= new ArrayList<SelectItem>();
			for(PartnerProductType t:list){
				SelectItem item = new SelectItem(t.getPartnerProductTypeId(),t.getCode()+" "+t.getName());
				typeList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return typeList;
	}
	public List<SelectItem> getCatList() {
		try{
			List<CalendarCategory> list = service.getCalendarCategoryList();
			catList= new ArrayList<SelectItem>();
			for(CalendarCategory c:list){
				SelectItem item = new SelectItem(c.getCalendarCatId(),c.getCode()+" "+c.getName());
				catList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return catList;
	}
	public List<PartnerProduct> getProductList() {
		try{
			productList = service.getPartnerProductList(partner);
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return productList;
	}
	
	public String navCreateAction(){
		product = new PartnerProduct();
		return NAV_CREATION;
	}
	public void navPopulateListActionEvent(ActionEvent actionEvent){
		try{
			this.serviceTypeList=new ArrayList<SelectItem>();
			List<XSServiceType> serviceTypeList=service.getAllServiceTypeList();
			for(XSServiceType serviceType : serviceTypeList){
				SelectItem item = new SelectItem(serviceType.getServiceTypeId(),serviceType.getServiceTypeDesc());
				this.serviceTypeList.add(item);
			}
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public String createAction(){
		try{
			product.setCalCat(new CalendarCategory(catId));
			product.setProductType(new PartnerProductType(this.typeId));
			service.createPartnerProduct(product, getThisUser(), partner);
			return NAV_LIST_PRODUCT;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_CREATION;
	}
	
	public String navListAction(){
		if(partner==null){
			PartnerBean bean = (PartnerBean)FacesUtils.getManagedBean(PartnerBean.BEAN_NAME);
			partner = bean.getPartner();
		}
		return NAV_LIST_PRODUCT;
	}
	
	public void navInventoryActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("productId2");
		String id = (String)param.getValue();
		for(PartnerProduct prod:productList){
			if(prod.getPartnerProductId().equalsIgnoreCase(id)){
				product = prod;
				break;
			}
		}
	}
	
	public String navInventoryAction(){
		PartnerInventoryBean bean = (PartnerInventoryBean)FacesUtils.getManagedBean(PartnerInventoryBean.BEAN_NAME);
		bean.setPartnerProduct(product);
		return bean.NAV;
	}
	
	public void navExtensionActionListener(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("productId");
		String id = (String)param.getValue();
		for(PartnerProduct prod:productList){
			if(prod.getPartnerProductId().equalsIgnoreCase(id)){
				product = prod;
				break;
			}
		}
	}
	
	public String navExtensionAction(){
		PartnerProductExtBean bean = (PartnerProductExtBean)FacesUtils.getManagedBean(PartnerProductExtBean.BEAN_NAME);
		return bean.navListAction(product);
	}
}
